#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/sockets.h"
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "iot_gpio.h"
#include "hi_io.h"
#include "iot_pwm.h"
#include "iot_errno.h"
#include "hi_adc.h"
#include "cJSON.h"
#include "aht20.h"
#include "hi_i2c.h"

#define LED_TEST_GPIO 9 // for hispark_pegasus

#define MY_PORT     3861
#define SERVER_PORT 3861

char g_recvline[1024];
int g_sockfd;
struct sockaddr_in g_servaddr;


void aht20_init(void)
{
    
    ssize_t retval = 0;
    
    hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
    hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);

    hi_i2c_init(HI_I2C_IDX_0, 400*1000);

    retval = AHT20_Calibrate();
    printf("AHT20_Calibrate: %d\r\n", retval);

}

void udp_thread(void)
{
    cJSON *data = NULL;
    char *result = NULL;
    int ret = 0;
    float temp = 0.0, humi = 0.0;
    ssize_t retval = 0;
    char stemp[10];
    char shumi[10];
    
    while(1)
    {
        
        retval = AHT20_StartMeasure();
        printf("AHT20_StartMeasure: %d\r\n", retval);

        retval = AHT20_GetMeasureResult(&temp, &humi);
        printf("AHT20_GetMeasureResult: %d, temp = %.2f, humi = %.2f\r\n", retval, temp, humi);

        sprintf(stemp,"%.2f",temp);
        sprintf(shumi,"%.2f",humi);
        
        data = cJSON_CreateObject();
        //温度
        cJSON_AddStringToObject(data, "temp", stemp);

        //湿度
        cJSON_AddStringToObject(data, "hum", shumi);
        
        result = cJSON_PrintUnformatted(data);
	    ret = strlen(result);

        retval = sendto(g_sockfd, result, ret, 0, (struct sockaddr *)&g_servaddr,sizeof(g_servaddr));
        if (retval < 0) {
            printf("sendto failed!\r\n");
        }
        printf("send UDP message {%s} %ld done!\r\n", result, ret);

    	cJSON_Delete(data);
    	free(result);

        sleep(3);
    }
}


void start_udp_thread(void)
{
    osThreadAttr_t attr;

    attr.name = "wifi_config_thread";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 2048;
    attr.priority = 36;

    if (osThreadNew((osThreadFunc_t)udp_thread, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}


void udp_demo(void)
{
    aht20_init(); // 初始化

    g_sockfd = socket(AF_INET, SOCK_DGRAM, 0);  //udp

    //server ip port
    struct sockaddr_in client_addr;

	// UDP socket 是 “无连接的” ，因此每次发送都必须先指定目标主机和端口，主机可以是多播地址
    bzero(&g_servaddr,sizeof(g_servaddr));
    g_servaddr.sin_family = AF_INET;
    g_servaddr.sin_port = htons(SERVER_PORT);
    g_servaddr.sin_addr.s_addr = inet_addr("255.255.255.255"); // 可设置成DAYU200ip
    
    bzero(&client_addr,sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(MY_PORT);
    client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    printf("addr: %X\n",client_addr.sin_addr.s_addr);
    
    if(bind(g_sockfd, (struct sockaddr *)&client_addr,sizeof(client_addr))<0)
    {
        printf("video rtcp bind ret < 0\n");
    }

    int opt = 1;
    int ret = setsockopt(g_sockfd,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt)); 

    start_udp_thread();

}

